package com.heima.common.aliyun;

import com.aliyun.green20220302.Client;
import com.aliyun.teaopenapi.models.Config;
import jdk.nashorn.internal.objects.annotations.Property;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @author shiminghui
 * @date 2024/11/4 18:47
 * @description: TODO
 */
@Data
@Component
@Slf4j
@ConfigurationProperties(prefix = "aliyun")
public class AliyunClient {
    private String accessKeyId;
    private String accessKeySecret;
    private String scenes;

    @Bean(name = "aliyunClient")
    public Client getClient() {
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        //接入区域和地址请根据实际情况修改
        config.setRegionId("cn-shanghai");
        config.setEndpoint("green-cip.cn-shanghai.aliyuncs.com");
        //连接时超时时间，单位毫秒（ms）。
        config.setReadTimeout(6000);
        //读取时超时时间，单位毫秒（ms）。
        config.setConnectTimeout(3000);
        //设置http代理。
        //config.setHttpProxy("http://10.10.xx.xx:xxxx");
        //设置https代理。
        //config.setHttpsProxy("https://10.10.xx.xx:xxxx");
        // 注意，此处实例化的client请尽可能重复使用，避免重复建立连接，提升检测性能
        Client client = null;
        try {
            client = new Client(config);
        } catch (Exception e) {
            log.error("创建aliyun client失败");
            throw new RuntimeException(e);
        }
        log.info("创建aliyun client成功");
        return client;
    }
}
